package com.example.clock.planPage;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.clock.Export.ExportData;
import com.example.clock.R;
import com.example.clock.basicClass.Plan;
import com.example.clock.basicClass.Task;
import com.example.clock.controlClass.DatabaseHelper;
import com.example.clock.controlClass.SqlController;

import java.util.ArrayList;

public class DetailPlan extends AppCompatActivity {
    Plan p;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_plan);

        Intent intent = getIntent();
        String planId = intent.getStringExtra("planid");

        DatabaseHelper dbhelper = new DatabaseHelper(DetailPlan.this,DetailPlan.this);
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        p = SqlController.findPlanById(db,planId);//获取到了Plan的Id
        //获取与该plan绑定的所有Task
        ArrayList<Task> arrayList = SqlController.getTaskByPlan(db,planId);

        TextView t1 = (TextView) findViewById(R.id.textView41);
        TextView t2 = (TextView) findViewById(R.id.textView43);
        TextView t3 = (TextView) findViewById(R.id.textView42);
        TextView t4 = (TextView) findViewById(R.id.textView39);
        TextView t5 = (TextView) findViewById(R.id.textView40);
        TextView t6 = (TextView) findViewById(R.id.textView44);
        TextView t7 = (TextView) findViewById(R.id.textView45);

        t1.setText(p.getPlanName());
        t2.setText(p.getLargeTags());
        t3.setText(p.getSmallTags());
        t4.setText("开始时间："+p.getStartDate());
        t5.setText("结束时间"+p.getEndDate());
        t6.setText("累计打卡："+p.getFinishTimes()+"次"+"/"+p.getPreTimes()+"次");
        t7.setText("累计时长："+p.getLastTime()+"min");


        //加载listview
        ListView listView = findViewById(R.id.listviewplantask);
        TaskPlanAdapter tpadapter = new TaskPlanAdapter(DetailPlan.this,R.layout.list_item_task_plan,arrayList);
        listView.setAdapter(tpadapter);

    }

    public void export(View v){
        Toast toast = new Toast(DetailPlan.this);
        SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
        String s = ExportData.exportTaskByPlanToCSV(db,this,p.getPlanId(),p.getPlanName());
        toast.setText("文件导出："+s);
        toast.show();
    }
    public void exportXlsx(View v){
        Toast toast = new Toast(DetailPlan.this);
        SQLiteDatabase db = new DatabaseHelper(this,this).getWritableDatabase();
        String s = ExportData.convertCsvToExcel(ExportData.exportTaskByPlanToCSV(db,this,p.getPlanId(),p.getPlanName()));
        toast.setText("文件导出："+s);
        toast.show();
    }
}